//
//  VIZAddColockController.m
//  Young-Old
//
//  Created by 位立志 on 16/4/29.
//  Copyright © 2016年 位立志. All rights reserved.
//

#import "VIZAddColockController.h"
#import "VIZTipController.h"
@interface VIZAddColockController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *items;

@end

@implementation VIZAddColockController

- (void)viewDidLoad {
    [super viewDidLoad];
//    VIZTipController *vc=[[VIZTipController alloc]init];
//    vc.aa=^(NSString *a){
//      
//    };
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}


#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *item = self.items[indexPath.row];
    
    VIZAddColockCell *cell = [VIZAddColockCell cellWithTableView:tableView andItem:item];
    
    cell.item = item;
    cell.backgroundColor = [UIView randomColor];
    return cell;
    
}


#pragma mark - 选中的单元格的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.items[indexPath.row];
    
    //获取cell
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    //控制器跳转
    if ([dict[VIZTargetController] length]>0) {
        NSString *controllerStr = dict[VIZTargetController];
        
        // 2.转为OC的类
        Class className = NSClassFromString(controllerStr);
        
        // 3.创建对象
        UIViewController *obj = [[className alloc] init];
        //设置头
        [obj.navigationItem setTitle:dict[VIZTitle]];
        
        if([obj isKindOfClass:[VIZAddColockController class]]){
            
            VIZAddColockController *newController = (VIZAddColockController*)obj;
            
            newController.plist = dict[VIZTargetPlist];
            [self.navigationController pushViewController:newController animated:YES];
        }else{
            
            VIZTipController *tipController = (VIZTipController*)obj;
            tipController.tipStr = ^(NSString *tip){
                
                if (tip != nil) {
                    
                    [self.items[indexPath.row] setObject:tip forKey:@"11"];
                }
                
            };
            
            [self.navigationController pushViewController:obj animated:YES];
            
            
        }
        
        
    }
    
    
    
}





#pragma mark - 懒加载items
- (NSArray *)items{
    
    if (_items == nil) {
        _items = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.plist ofType:@"plist"]];
    }
    
    return _items;

}










@end
